package g11.dao;

import g11.model.Drug;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class DrugDAOImpl implements DrugDAO {

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session.isOpen())
			session.close();
		// HibernateUtil.shutdown();
	}

	public DrugDAOImpl() {

	}

	public Integer createDrug(Drug drug) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		int drugId;
		try {
			tran = session.beginTransaction();
			drugId = (Integer) session.save(drug);
			tran.commit();
			return drugId;
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public void updateDrug(Drug drug) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(drug);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public void deleteDrug(Drug drug) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.delete(drug);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public Drug getDrugById(int drugId) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Drug drug = null;
		try {
			String hql = "from Drug d where d.drugId=?";
			Query q = session.createQuery(hql);
			q.setInteger(0, drugId);
			if (q.list().size() > 0) {
				drug = (Drug) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return drug;
	}

	public Drug getDrugByName(String drugName) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Drug drug = null;
		try {
			String hql = "from Drug d where d.drugName=?";
			Query q = session.createQuery(hql);
			q.setString(0, drugName);
			if (q.list().size() > 0) {
				drug = (Drug) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return drug;

	}

	@Override
	public List<Drug> getAllDrugs() throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		List<Drug> drugList = new ArrayList<Drug>();
		try {
			Query hql = session.createQuery("from Drug");
			drugList = hql.list();
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return drugList;
	}

}
